home *** CD-ROM | disk | FTP | other *** search
/ Delphi Developer's Kit 1996 / Delphi Developer's Kit 1996.iso / power / wfc007.000 / src / cnetconn.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1995-12-22  |  5.1 KB  |  221 lines

  1. #include <wfc.h>
  2. #pragma hdrstop
  3.  
  4. /*
  5. ** Author: Samuel R. Blackburn
  6. ** CI$: 76300,326
  7. ** Internet: sammy@sed.csc.com
  8. **
  9. ** You can use it any way you like.
  10. */
  11.  
  12. #if defined( _DEBUG )
  13. #undef THIS_FILE
  14. static char BASED_CODE THIS_FILE[] = __FILE__;
  15. #endif
  16.  
  17. /*
  18. ** CNetworkConnectionInformation stuff
  19. */
  20.  
  21. IMPLEMENT_SERIAL( CNetworkConnectionInformation, CObject, 1 )
  22.  
  23. CNetworkConnectionInformation::CNetworkConnectionInformation()
  24. {
  25.    m_Initialize();
  26. }
  27.  
  28. CNetworkConnectionInformation::CNetworkConnectionInformation( CONNECTION_INFO_1 *source )
  29. {
  30.    Copy( source );
  31. }
  32.  
  33. CNetworkConnectionInformation::~CNetworkConnectionInformation()
  34. {
  35.    m_Initialize();
  36. }
  37.  
  38. void CNetworkConnectionInformation::Copy( CONNECTION_INFO_1 *source )
  39. {
  40.    ASSERT( source != NULL );
  41.  
  42.    if ( source == NULL )
  43.    {
  44.       m_Initialize();
  45.       return;
  46.    }
  47.  
  48. #if ! defined( UNICODE )
  49.    ::UNICODE_to_ASCII( (LPCWSTR) source->coni1_username, source->coni1_username );
  50.    ::UNICODE_to_ASCII( (LPCWSTR) source->coni1_netname,  source->coni1_netname  );
  51. #endif
  52.  
  53.    ID            = source->coni1_id;
  54.    Type          = source->coni1_type;
  55.    NumberOfOpens = source->coni1_num_opens;
  56.    NumberOfUsers = source->coni1_num_users;
  57.    Time          = source->coni1_time;
  58.    UserName      = source->coni1_username;
  59.    NetName       = source->coni1_netname;
  60.  
  61. #if ! defined( UNICODE )
  62.    ::ASCII_to_UNICODE( source->coni1_username, (LPWSTR) source->coni1_username );
  63.    ::ASCII_to_UNICODE( source->coni1_netname,  (LPWSTR) source->coni1_netname  );
  64. #endif
  65. }
  66.  
  67. void CNetworkConnectionInformation::Empty( void )
  68. {
  69.    m_Initialize();
  70. }
  71.  
  72. void CNetworkConnectionInformation::m_Initialize( void )
  73. {
  74.    UserName.Empty();
  75.    NetName.Empty();
  76.    ID            = 0;
  77.    Type          = 0;
  78.    NumberOfUsers = 0;
  79.    NumberOfOpens = 0;
  80.    Time          = 0;
  81. }
  82.  
  83. void CNetworkConnectionInformation::Serialize( CArchive& archive )
  84. {
  85.    CObject::Serialize( archive );
  86.  
  87.    if ( archive.IsStoring() )
  88.    {
  89.       archive << ID;
  90.       archive << Type;
  91.       archive << NumberOfOpens;
  92.       archive << NumberOfUsers;
  93.       archive << UserName;
  94.       archive << NetName;
  95.    }
  96.    else
  97.    {
  98.       archive >> ID;
  99.       archive >> Type;
  100.       archive >> NumberOfOpens;
  101.       archive >> NumberOfUsers;
  102.       archive >> UserName;
  103.       archive >> NetName;
  104.    }
  105. }
  106.  
  107. /*
  108. ** CNetworkConnections Stuff
  109. */
  110.  
  111. IMPLEMENT_SERIAL( CNetworkConnections, CNetwork, 1 )
  112.  
  113. CNetworkConnections::CNetworkConnections()
  114. {
  115.    m_Initialize();
  116. }
  117.  
  118. CNetworkConnections::CNetworkConnections( LPCTSTR machine_name )
  119. {
  120.    m_Initialize();
  121.    Open( machine_name );
  122. }
  123.  
  124. CNetworkConnections::~CNetworkConnections()
  125. {
  126.    Close();
  127.    m_Initialize();
  128. }
  129.  
  130. void CNetworkConnections::Close( void )
  131. {
  132.    CNetwork::Close();
  133.  
  134.    if ( m_1InformationBuffer != NULL )
  135.    {
  136.       ::NetApiBufferFree( m_1InformationBuffer );
  137.       m_1InformationBuffer = NULL;
  138.    }
  139. }
  140.  
  141. void CNetworkConnections::m_Initialize( void )
  142. {
  143.    m_ErrorCode               = 0;
  144.    m_1InformationBuffer    = NULL;
  145.    m_1ResumeHandle         = 0;
  146.    m_1CurrentEntryNumber   = 0;
  147.    m_1NumberOfEntriesRead  = 0;
  148.    m_1TotalNumberOfEntries = 0;
  149. }
  150.  
  151. BOOL CNetworkConnections::Enumerate( LPCTSTR share_or_computer_name )
  152. {
  153.    if ( m_1InformationBuffer != NULL )
  154.    {
  155.       ::NetApiBufferFree( m_1InformationBuffer );
  156.       m_1InformationBuffer = NULL;
  157.    }
  158.  
  159.    if ( share_or_computer_name == NULL )
  160.    {
  161.       m_ErrorCode = ERROR_INVALID_PARAMETER;
  162.       return( FALSE );
  163.    }
  164.  
  165.    LPWSTR wide_share_or_computer_name[ MAX_PATH ];
  166.  
  167. #if ! defined( UNICODE )
  168.    ASCII_to_UNICODE( share_or_computer_name, (LPWSTR) wide_share_or_computer_name );
  169. #else
  170.    strcpy( wide_share_or_computer_name, share_or_computer_name );
  171. #endif // UNICODE
  172.  
  173.    m_1CurrentEntryNumber   = 0;
  174.    m_1NumberOfEntriesRead  = 0;
  175.    m_1ResumeHandle         = 0;
  176.    m_1TotalNumberOfEntries = 0;
  177.  
  178.    m_ErrorCode = ::NetConnectionEnum( (LPTSTR) m_WideMachineName,
  179.                                       (LPTSTR) wide_share_or_computer_name, 
  180.                                                1, 
  181.                                    (LPBYTE *) &m_1InformationBuffer,
  182.                                                65535,
  183.                                               &m_1NumberOfEntriesRead,
  184.                                               &m_1TotalNumberOfEntries,
  185.                                               &m_1ResumeHandle );
  186.  
  187.    if ( m_ErrorCode != NERR_Success || m_1InformationBuffer == NULL )
  188.    {
  189.       return( FALSE );
  190.    }
  191.  
  192.    return( TRUE );
  193. }
  194.  
  195. BOOL CNetworkConnections::GetNext( CNetworkConnectionInformation& information )
  196. {
  197.    if ( m_1CurrentEntryNumber < m_1TotalNumberOfEntries )
  198.    {
  199.       information.Copy( &m_1InformationBuffer[ m_1CurrentEntryNumber ] );
  200.       m_1CurrentEntryNumber++;
  201.       return( TRUE );
  202.    }
  203.  
  204.    information.Empty();
  205.    return( FALSE );
  206. }
  207.  
  208. void CNetworkConnections::Serialize( CArchive& archive )
  209. {
  210.    CNetwork::Serialize( archive );
  211.  
  212.    if ( archive.IsStoring() )
  213.    {
  214.       archive << m_ErrorCode;
  215.    }
  216.    else
  217.    {
  218.       archive >> m_ErrorCode;
  219.    }
  220. }
  221.